package com.demo.web.pc;

import com.demo.common.Constants;
import com.demo.dao.ApplySendMapper;
import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.*;
import com.demo.service.*;
import com.demo.service.*;
import com.demo.vo.OrderVo;
import com.demo.vo.SalePersonVolumeOfTrade;
import com.demo.entity.Order;
import com.demo.entity.SalePerson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderPcController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private IOrderService depService;
    @Autowired
    private IOfferService offerService;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IClienteleService clienteleService;
    @Autowired
    private IUserWxService userWxService;
    @Autowired
    private ApplySendMapper asmDao;

    @ResponseBody
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public BaseResult<Object> test(HttpSession session) {
        SalePerson salep = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
//        if (salep == null) {
//            return new BaseResult(false, "未登录 ", 401);
//        }
//        userWxService
//        if (salep.getOpenid() != null) {
//            UserWx uw = new UserWx();
//            uw.setUnionid(salep.getOpenid());
//            uw.setUnitid(Constants.WXUserTypeWX);
//            UserWx ux1 = userWxService.selectByUnionid(uw);
//            if (ux1 != null && ux1.getOpenid() != null) { //系统包含微信账户
//
//                AdvancedUtil.senderTmplSms(ux1.getOpenid());
//            }
//        }
        depService.createTest();
        return new BaseResult(false, "Success", 500);
    }

    /**
     * 创建
     * todo:事务处理
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public BaseResult<Object> get(Order arg, HttpSession session) {
        SalePerson salep = (SalePerson) session.getAttribute(Constants.CURRENT_SP);

        if (salep == null) {
            return new BaseResult(false, "未登录 ", 401);
        }

        try {
            arg.setSaleId(String.valueOf(salep.getId()));
            arg.setSaleName(salep.getUserName());

            Object str = depService.create(arg);

            BaseResult res = null;
            if (str instanceof String) {
                res = new BaseResult(false, str.toString(), 403);
            } else {
                res = new BaseResult(true, str);
            }
            return res;
        } catch (Exception e) {
            logger.error(e.getStackTrace().toString());
            return new BaseResult(false, "服务端错误!", 500);
        }

    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<Order> query(OrderVo arg, HttpSession session) {
        SalePerson sp = (SalePerson) session.getAttribute(Constants.CURRENT_SP);
        if (sp == null) {
            return new ListResult(false, "未登录!", 401);
        }
        arg.setSaleId(String.valueOf(sp.getId()));
        List<OrderVo> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<Order> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /***
     * 我的客户订单
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMyOrder", method = RequestMethod.GET)
    public ListResult<Object> queryMyOrder(OrderVo arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        ListResult<Object> res;
        if (sale != null) {
            arg.setSaleId(String.valueOf(sale.getId()));
        } else {
            res = new ListResult(false, "请登录!", 401);
            return res;
        }
        if ("MY".equals(arg.getClienteleId())) { // 我的可发货客户订单
            arg.setClienteleId(asmDao.selectBySaleId(String.valueOf(sale.getId())).getClienteleId());
        }
        List<OrderVo> list = depService.queryMyOrdersByPage(arg);
        int total = depService.queryMyOrderCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }


    @ResponseBody
    @RequestMapping(value = "/queryMyAccount", method = RequestMethod.GET)
    public ListResult<Object> queryMyAccount(SalePersonVolumeOfTrade arg, HttpSession session) {

        SalePerson sale = (SalePerson) session.getAttribute("current");
        ListResult<Object> res;
        if (sale != null) {
            arg.setId(String.valueOf(sale.getId()));
        } else {
            res = new ListResult(false, "请登录!", 401);
            return res;
        }
        SalePersonVolumeOfTrade total = depService.queryMyAccountSummary(arg);
        res = new ListResult(true, total);
        return res;
    }


    /**
     * 订单详情
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Order> get(Order arg) {
        Order service_res = depService.selectByPrimaryKey(arg.getId());
        BaseResult<Order> res = new BaseResult<Order>(true, service_res);
        return res;
    }

    /**
     * 我的销售员订单
     *
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMySaleOrder", method = RequestMethod.GET)
    public ListResult<Object> queryMySaleOrder(OrderVo arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        ListResult<Object> res;
        if (sale != null) {
            arg.setSaleId(String.valueOf(sale.getId()));
        } else {
            res = new ListResult(false, "请登录!", 401);
            return res;
        }
        List<OrderVo> list = depService.queryMySalePersonOrdersByPage(arg);
        int total = depService.queryMySaleOrderCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }

    /**
     * 我的销售 概况
     *
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryMySaleOrderTotal", method = RequestMethod.GET)
    public ListResult<Object> queryMySaleOrderTotal(SalePersonVolumeOfTrade arg, HttpSession session) {
        SalePerson sale = (SalePerson) session.getAttribute("current");
        ListResult<Object> res;
        if (sale != null) {
            arg.setId(String.valueOf(sale.getId()));
        } else {
            res = new ListResult(false, "请登录!", 401);
            return res;
        }
        List<SalePersonVolumeOfTrade> list = depService.queryMySaleAccountSummary(arg);
        res = new ListResult(true, list);
        return res;
    }


}
